package com.ebt.m.proposal_v2.ui.function;

import android.content.Context;

import com.ebt.m.data.entity.Customer;
import com.ebt.m.proposal_v2.bean.ApplicantEntity;
import com.ebt.m.proposal_v2.bean.InsuredEntity;
import com.ebt.m.proposal_v2.bean.event.ExemptCompleteEvent;
import com.ebt.m.proposal_v2.helper.PickerHelper;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.proposal_v2.widget.view.InsuranceCellView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 单任务执行模式
 *
 * @author archer.qi
 *         Created on 2017/7/18.
 */
public abstract class SingleWorkerForExemptRisk {

    public Context mContext;
    private EBTProgressDialog progressDialog;
    public ExemptTask mRunningTask; // 正在执行的任务
    public List<ExemptTask> mWaitingTasks; // 等待的任务

    /**
     * 创建一个新任务
     *
     * @param applicant
     * @param insured
     * @param view
     */
    public void createNewTask( Customer applicant, Customer insured, InsuranceCellView view) {
        if (applicant == null || insured == null || view == null) {
            completeAllTask(true, true);
            return;
        }

        ExemptTask newTask = new ExemptTask();
        newTask.applicant = applicant;
        newTask.insured = insured;
        newTask.cellView = view;

        if (mRunningTask == null) {
            if (mWaitingTasks == null || mWaitingTasks.size() == 0) {
                // 没有任务正在运行 && 没有等待任务 -> 运行当前任务
                startTask(newTask);
            } else {
                // 没有任务正在运行 && 有任务正在等待 -> 先执行等待任务，并加入等待，遵从先进先出原则
                ExemptTask mPrepareRunningTask = popTask();
                startTask(mPrepareRunningTask);
                waitingTask(newTask);
            }
        } else {
            // 有任务正在执行 -> 加入等待队列
            waitingTask(newTask);
        }
    }

    public void createNewTask(ApplicantEntity applicantEntity, InsuredEntity insuredEntity, Customer applicant, Customer insured, InsuranceCellView view) {
        if (applicant == null || insured == null || view == null) {
            completeAllTask(true, true);
            return;
        }

        ExemptTask newTask = new ExemptTask();
        newTask.applicant = applicant;
        newTask.insured = insured;
        newTask.cellView = view;

        if (mRunningTask == null) {
            if (mWaitingTasks == null || mWaitingTasks.size() == 0) {
                // 没有任务正在运行 && 没有等待任务 -> 运行当前任务
                startTask(newTask);
            } else {
                // 没有任务正在运行 && 有任务正在等待 -> 先执行等待任务，并加入等待，遵从先进先出原则
                ExemptTask mPrepareRunningTask = popTask();
                startTask(mPrepareRunningTask);
                waitingTask(newTask);
            }
        } else {
            // 有任务正在执行 -> 加入等待队列
            waitingTask(newTask);
        }
    }

    /**
     * 加入等待队列
     *
     * @param task
     */
    private void waitingTask(ExemptTask task) {
        if (task == null) {
            return;
        }
        if (mWaitingTasks == null) {
            mWaitingTasks = new ArrayList<>();
        }
        mWaitingTasks.add(task);
    }

    /**
     * 执行任务
     *
     * @param task
     */
    private void startTask(ExemptTask task) {
        if (task == null) {
            return;
        }
        mRunningTask = task;
        showProgress();
        start(mRunningTask);
    }

    /**
     * 弹出一个待执行任务
     *
     * @return
     */
    private ExemptTask popTask() {
        if (mWaitingTasks == null || mWaitingTasks.size() == 0) {
            return null;
        }
        ExemptTask mPrepareRunningTask = mWaitingTasks.get(0);
        mWaitingTasks.remove(0);
        return mPrepareRunningTask;
    }

    /**
     * 是否有正在运行的任务
     *
     * @return
     */
    private boolean isRunning() {
        if (mRunningTask == null) {
            return false;
        }
        return true;
    }

    /**
     * 任务执行结束调用，取出一个待执行任务，并执行
     */
    public void recycleTask() {
        if (mRunningTask != null) {
            mRunningTask = null;
        }
        ExemptTask mPrepareRunningTask = popTask();
        if (mPrepareRunningTask != null) {
            startTask(mPrepareRunningTask);
        } else {
            // 如果没有等待任务，则执行完毕
            completeAllTask(true, true);
        }
    }

    /**
     * 所有任务均已完成
     */
    public void completeAllTask(boolean refreshPremium, boolean verifyProposal) {
        dismissProgress();
        EventBus.getDefault().post(new ExemptCompleteEvent(refreshPremium, verifyProposal));
    }

    /**
     * 清空所有任务
     */
    public void clearTasks() {
        dismissProgress();
        mRunningTask = null;
        if (mWaitingTasks != null) {
            mWaitingTasks.clear();
            mWaitingTasks = null;
        }
    }

    public void showProgress() {
        /*if (progressDialog == null || (progressDialog != null && !progressDialog.isShowing())) {
            progressDialog = PickerHelper.showProgress(mContext, "重新计算");
        }*/
        if (progressDialog == null) {
            progressDialog = new EBTProgressDialog(mContext);
        }
        progressDialog.setMessage("重新计算");
        if (!progressDialog.isShowing()){
            progressDialog.show();
        }
    }

    public void dismissProgress() {
        /*if (progressDialog != null) {
            progressDialog.dismiss();
        }*/
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    public abstract void start(ExemptTask task);
}
